﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    public class MsgManageClass
    {
        public event EventHandler<MsgManageEventArgs> NewMsg;

        public void OnMsgEvent(MsgManageEventArgs e)
        {
            EventHandler<MsgManageEventArgs> eh = NewMsg;
            if (eh != null)
            {
                eh(this, e);
            }
        }

        public void SendMsg(string msg)
        {
            MsgManageEventArgs e = new MsgManageEventArgs(ToString(), msg);
            OnMsgEvent(e);
        }

        private readonly object m_eventLocker = new object();
        private EventHandler<MsgManageEventArgs> m_StrongNewMsg;
        public event EventHandler<MsgManageEventArgs> StrongNewMsg
        {
            add 
            {
                lock (m_eventLocker)
                {
                    m_StrongNewMsg += value;
                }
            }
            remove
            {
                lock (m_eventLocker)
                {
                    m_StrongNewMsg -= value;
                }
            }
        }
    }
    public class MsgManageEventArgs : EventArgs
    {
        public string Message { get; set; }
        public string From { get; set; }
        public MsgManageEventArgs(string from,string msg)
        {
            From = from;
            Message = msg;
        }
    }
}
